package com.banyou.banyou.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banyou.banyou.R;
import com.banyou.banyou.bean.main.search.SearchListBean;
import com.banyou.banyou.util.FontDisplayUtil;
import com.banyou.banyou.util.GlideUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * 项目名称：BanYou
 * 包名:com.banyou.banyou.adapter
 * 类描述：
 * 创建人：LX
 * 创建时间：2017/5/22 下午4:12
 * 修改人：LX
 * 修改时间：2017/5/22 下午4:12
 * 修改备注：
 */

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchViewHolder> {

    private Context mContext;
    private List<SearchListBean> mList;

    public SearchAdapter(Context context, List<SearchListBean> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_search_item, parent, false);
        SearchViewHolder holder = new SearchViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(SearchViewHolder holder, int position) {
        int fixPosition=position;
        GlideUtil.nomalLoad(mContext, mList.get(fixPosition).getPORTAL_IMAG_PATH(), holder.mImage);
        holder.mAge.setText("年龄："+mList.get(fixPosition).getAGE());
        holder.mName.setText("姓名："+mList.get(fixPosition).getUSER_NAME());
        holder.mCity.setText("地区："+mList.get(fixPosition).getPROVINCE_NAME()+"-"+mList.get(fixPosition).getREGION_NAME());
        if(mList.get(fixPosition).getSEX()!=null&&mList.get(fixPosition).getSEX().equals("M")){
            holder.mSex.setText("性别：男");
        }else if(mList.get(fixPosition).getSEX()!=null&&mList.get(fixPosition).getSEX().equals("F")){
            holder.mSex.setText("性别：女");
        }else{
            holder.mSex.setText("性别：无");
        }
        holder.mStarContnet.removeAllViews();
        for (int i = 0; i < mList.get(fixPosition).getLEVEL(); i++) {
            ImageView imageView=new ImageView(mContext);
            imageView.setImageResource(R.drawable.star);
            imageView.setLayoutParams(new LinearLayoutCompat.LayoutParams(FontDisplayUtil.dip2px(16),FontDisplayUtil.dip2px(16)));
            holder.mStarContnet.addView(imageView);
        }
        if(mList.get(fixPosition).getLEVEL()==0){
            TextView textView=new TextView(mContext);
            textView.setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            textView.setText("无");
            holder.mStarContnet.addView(textView);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class SearchViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_head)
        ImageView mImage;
        @BindView(R.id.name)
        TextView mName;
        @BindView(R.id.age)
        TextView mAge;
        @BindView(R.id.star_contnet)
        LinearLayout mStarContnet;
        @BindView(R.id.city)
        TextView mCity;
        @BindView(R.id.sex)
        TextView mSex;
        public SearchViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(v,getLayoutPosition());
                    }
                }
            });
        }
    }

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View v,int position);
    }
}
